﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Marbles.Core.Connectors;
using Marbles.Core.Content.Sites;

namespace Marbles.Core.Content.Banners
{
    public class BannerCollection : ICollection<Banner>
    {

        public Banner this[int index] { get { return items[index]; } set { items[index] = value; } }
        public Int32 Count { get { return items.Count; } }
        public bool IsReadOnly { get { return false; } }

        private List<Banner> items = null;
        private SiteDefinition _web = null;

        public BannerCollection(SiteDefinition Web)
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("SELECT * FROM Banners WHERE siteid = @siteid AND active = 1");
            cmd.AddParameter("@siteid", Web.SiteId);
            _web = Web;
            items = new List<Banner>();
            foreach (DataRow dr in dbc.FetchDataTable(cmd).Rows)
            {
                items.Add(new Banner(dr));
            }
        }

        public void Update()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("DELETE FROM Banners WHERE siteid = @siteid");
            int bannerid = 0;
            cmd.AddParameter("@siteid", _web.SiteId);
            dbc.ExecuteCommand(cmd);

            foreach (Banner b in items)
            {
                b.BannerId = bannerid;
                cmd = new Command("INSERT INTO Banners (siteid, id, imageurl, navigateurl, alternatetext, keyword, impressions, width, height, active)  VALUES (@siteid, @id, @imageurl, @navigateurl, @alternatetext, @keyword, @impressions, @width, @height, @active)");
                cmd.AddParameter("@siteid", _web.SiteId);
                cmd.AddParameter("@id", b.ID);
                cmd.AddParameter("@imageurl", b.ImageUrl);
                cmd.AddParameter("@navigateurl", b.NavigateUrl);
                cmd.AddParameter("@alternatetext", b.AlternateText);
                cmd.AddParameter("@keyword", b.Keyword);
                cmd.AddParameter("@impressions", b.Impressions);
                cmd.AddParameter("@width", b.Width);
                cmd.AddParameter("@height", b.Height);
                cmd.AddParameter("@active", b.Active);
                bannerid += 1;
            }
        }

        public static Int32 CountBanners(string SiteId)
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("SELECT COUNT(id) AS total Banners WHERE siteid = @siteid");
            cmd.AddParameter("@siteid", SiteId);
            DataTable dt = dbc.FetchDataTable(cmd);

            if (dt.Rows.Count > 0) { return Convert.ToInt32(dt.Rows[0]["total"]); }

            return 0;
        }

        public void Add(Banner item)
        {
            items.Add(item);
        }

        public Banner CreateNew()
        {
            return Banner.CreateNew(_web);
        }

        public void Clear()
        {
            items.Clear();
        }

        public bool Contains(Banner item)
        {
            return items.Contains(item);
        }

        public void CopyTo(Banner[] array, int arrayIndex)
        {
            items.CopyTo(array, arrayIndex);
        }

        public bool Remove(Banner item)
        {
            if (items.Remove(item)) { item.Delete(); return true; }
            return false;
        }

        public IEnumerator<Banner> GetEnumerator()
        {
            return items.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return items.GetEnumerator();
        }
    }
}
